<?PHP
	require 'includes/master.inc.php';
	require 'includes/ap.inc.php';

	$auth->admin();
	
	if(isset($_POST['btnsubmit']))
	{
		$Error->blank($_POST['first_name'], "first_name", "First Name");
		$Error->blank($_POST['last_name'], "last_name", "Last Name");
		$Error->email($_POST['email'], "email");

		$app = new App($_POST['app_id']);
		if($app->id == "")
			$Error->add("app_id", "Please select an app.");
		
		if($Error->ok())
		{
			$o = new Order();
			$o->type         = "Manual";
			$o->app_id       = $app->id;
			$o->first_name   = $_POST['first_name'];
			$o->last_name    = $_POST['last_name'];
			$o->payer_email  = $_POST['email'];
			$o->dt           = dater();
			$o->insert();
			$o->generateLicense();

			if($_POST['send_email'] == 1)
				$o->emailLicense();

			redirect("order.php?id=" . $o->id);
		}
		else
		{
			$app_id     = htmlspecialchars($_POST['app_id']);
			$first_name = htmlspecialchars($_POST['first_name']);
			$last_name  = htmlspecialchars($_POST['last_name']);
			$email      = htmlspecialchars($_POST['email']);
		}
	}
	else
	{
		$app_id     = isset($_GET['app_id']) ? $_GET['app_id'] : '';
		$first_name = '';
		$last_name  = '';
		$email      = '';
	}

	$a = new App();
	$apps = $a->glob("ORDER BY name");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title>Manual Order | Appcaster</title>
	<?PHP include 'inc/meta.inc.php'; ?>
	<?PHP $Error->css(); ?>
</head>

<body id="home">
	<?PHP include 'inc/head.inc.php'; ?>
	<div id="nav">
		<ul>
			<li><a href='<?PHP WEBROOT();?>index.php'>Applications</a></li>
			<li class='selected'><a href='<?PHP WEBROOT();?>orders.php'>Orders</a></li>
			<li><a href='<?PHP WEBROOT();?>feedback.php'>Feedback (<?PHP echo Feedback::newCountTotal();?>)</a></li>
			<li><a href='<?PHP WEBROOT();?>stats.php'>Stats</a></li>
			<li><a href='<?PHP WEBROOT();?>options.php'>Options</a></li>
		</ul>
	</div>

	<div id="menutabs">
		<ul id="tabnav">
			<li><a href='<?PHP WEBROOT();?>orders.php'>All Orders</a></li>
			<?PHP foreach($apps as $a) : ?>
			<li><a href="<?PHP WEBROOT();?>orders.php?app_id=<?PHP echo $a->id;?>"><?PHP echo $a->__name; ?></a></li>
			<?PHP endforeach; ?>
			<li class='selected'><a href='<?PHP WEBROOT();?>manualorder.php'>Manual Order</a></li>
		</ul>
	</div>

	<div id="contain">
		<form action="" method="post">
			<?PHP echo $Error; ?>
			<p>Application:
				<select name="app_id" id="app_id">
					<option value="">--Choose--</option>
					<?PHP echo get_options('apps', 'id', 'name', $app_id); ?>
				</select>
			</p>
			<p><label for="first_name">First Name:</label> <input type="text" class="text" name="first_name" value="<?PHP echo $first_name;?>" id="first_name"/></p>
			<p><label for="last_name">Last Name:</label> <input type="text" class="text" name="last_name" value="<?PHP echo $last_name;?>" id="last_name"/></p>
			<p><label for="email">Email:</label> <input type="text" class="text" name="email" value="<?PHP echo $email;?>" id="email"/></p>
			<p><input type="checkbox" name="send_email" value="1" id="send_email"/> <label for="send_email">Send license to user via email?</label></p>
			<p><input type="submit" name="btnsubmit" value="Create Order" id="btnsubmit"/></p>
		</form>
	</div>
</body>
</html>
